package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.chlid

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.bean.AgencyCenterAreaBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.bean.AgencyCountListItem
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.bean.AgencyMySpotItemBean
import com.senon.modularapp.util.JssSpannableString
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class AgencyCountListFragment : SimpleListFragment<AgencyCountListItem>() {

    companion object {
        fun newInstance(item: AgencyCenterAreaBean?): AgencyCountListFragment {
            val args = Bundle()
            args.putSerializable("item", item)
            val fragment = AgencyCountListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var item: AgencyCenterAreaBean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        item = arguments?.getSerializable("item") as AgencyCenterAreaBean
        isEnableLoadMore = false
        isAddDividerItemDecoration = false
    }


    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
//        addHeader(R.layout.comment_header_layout)
        addHeader(R.layout.fragment_agency_count_header_list)
        val mNavigation: View = mHeader.findViewById(R.id.mNavigation)
        mNavigation.setBackgroundResource(R.color.bg_color_0)

        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.title = item?.title
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
    }

    override fun getItemLayout(): Int {
        return R.layout.fragment_agency_count_list
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: AgencyCountListItem?) {
        val createTime = "${item?.createTime}"
        helper?.setText(R.id.areaName, "${item?.areaName}")
            ?.setText(R.id.realName, "${item?.account}")
            ?.setViewVisible(R.id.line, helper.layoutPosition <= ((mAdapter?.itemCount?:0) - 1))
            ?.setText(
                R.id.createTime, JssSpannableString(_mActivity, createTime)
                    .first(createTime).size(8)
            )
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(AgencyCountListItem::class.java).endSubType().build()
    }

    override fun netRequest() {
        RestClient.builder()
            .url(R.string.queryAreaProxy)
            .raw(
                JSON.toJSONString(
                    mapOf(
                        "areaType" to "${item?.type}"
                    )
                )
            )
            .success {
                parseDate(it)
            }.error { _, _ ->
                onFailed()
            }.failure {
                onFailed()
            }.build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val proxyList = obj["proxyList"] ?: ""
        val array = JSON.parseArray(JSON.toJSONString(proxyList), AgencyCountListItem::class.java)
        loadListDate(array)
    }
}